<template>
  <div class="login">
    <el-card>
      <div class="logo">
        <img src="../assets/logo.png" alt />
      </div>
      <el-form :model="loginForm" ref="loginForm" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" prefix-icon="el-icon-key"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">提交</el-button>
          <el-button class="reset" type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>;
export default {
  data() {
    return {
      // 登陆表单
      loginForm: {
        username: "admin",
        password: "123456"
      },
      // 表单校验规则
      rules: {
        username: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    login() {
      this.$refs.loginForm.validate(async valid => {
        if (!valid) return;
        let { data: res } = await this.$http.post("login", this.loginForm);
        // console.log(res);
        if (res.meta.status != 200) {
          window.sessionStorage.clear();
          return this.$message.error("账号或则密码错误");
        }
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
        this.$message.success('登陆成功');
      });
    },
    reset() {
      this.$refs.loginForm.resetFields();
    }
  }
};
</script>
<style lang="less" scoped>
.login {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
}
.el-card {
  width: 500px;
  height: 300px;
  position: fixed;
  left: 50%;
  top: 50%;
  margin-left: -250px;
  margin-top: -150px;
  overflow: initial;
}
.logo {
  position: relative;
  img {
    position: absolute;
    top: -90px;
    left: 50%;
    margin-left: -75px;
    width: 150px;
    height: 150px;
    background-color: #eeeeee;
    border-radius: 50%;
    border: 5px solid #ffffff;
    box-sizing: border-box;
    box-shadow: #fff 0px 0px 6px 8px;
  }
}
.el-form {
  margin-top: 100px;
}
.reset {
  float: right;
}
</style>
